package com.ghck.server.config;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.util.AntPathMatcher;

import com.ghck.server.model.UserRole;
import com.ghck.server.security.AjkAuthenticationFilter;


@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ComponentScan("com.ghck.server.security")
public class RestSecurityConfiguration extends WebSecurityConfigurerAdapter {
	public static AntPathMatcher antMatcher = new AntPathMatcher();
	public static boolean matchPermitAll(String path){
		if(pathMap!=null && !pathMap.isEmpty()){
			for(Entry<String, Integer> entry : pathMap.entrySet()){
				if(antMatcher.match(entry.getKey(), path)){
					if(entry.getValue()==CNT_ALL){
						return true;
					}
				}
			}
		}
		return false;
	}

	private static AjkAuthenticationFilter ajkAuthenticationFilter = new AjkAuthenticationFilter();
	public static Map<String, Integer> pathMap = new LinkedHashMap<String, Integer>();
	public static int CNT_PATIENT = 1;
	public static int CNT_DOCTOR=2;
	public static int CNT_ADMIN=4;
	public static int CNT_ALL = 8;
	static{  //添加的时候注意一下排序，字母倒叙的顺序添加
		pathMap.clear();
		

	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable();
		http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
		http.addFilterBefore(ajkAuthenticationFilter, BasicAuthenticationFilter.class);
	}
}